package com.example.custom.boot.starter.starter;

import com.example.custom.boot.starter.service.FruitService;
import com.example.custom.boot.starter.config.FruitConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * @author duanqichao
 * date: 2021/01/15 1:20
 */
@Configuration
@EnableConfigurationProperties(FruitAutoConfiguration.class)
@ConditionalOnClass()
@ComponentScan(basePackages = "com.example.custom.boot.starter.config")
@ConditionalOnProperty(prefix = "fruit", value = "enabled", matchIfMissing = true)
public class FruitAutoConfiguration {

    private final FruitConfig fruitConfig;

    public FruitAutoConfiguration(FruitConfig fruitConfig) {

        this.fruitConfig = fruitConfig;
    }

    @Bean
    public FruitService fruitService() {
        return new FruitService(fruitConfig);
    }
}
